<template>
	<view class="page">
		<view class="top-head">
			<view class="head-l">
				<image src="../../../static/jiaotou1.png" @click="gotoPage('/pages/user/my-driver/my-driver')" mode="">
				</image>
			</view>
			<view class="head-title">添加司机</view>
			<view class="head-r"></view>
		</view>
		<view class="add-name">
			<text>姓名</text>
			<input type="text" placeholder="请输入姓名" v-model="driver.name" />
		</view>
		<view class="add-name">
			<text>住址</text>
			<input type="text" placeholder="请输入住址" v-model="driver.address" />

		</view>
		<view class="add-name">
			<text>年龄</text>
			<input type="number" placeholder="请输入年龄" v-model="driver.age" />
		</view>
		<view class="add-name">
			<text>驾照类型</text>
			<input type="text" class="add-inp" placeholder="请选择" v-model="driver.addcar_type" disabled="" />
			<picker @change="seleceType" range-key="type_name" :range="selectType" :value="index">
				<image src="../../../static/select.png"></image>
			</picker>
		</view>
		<view class="add-name">
			<text>驾龄</text>
			<input type="text" class="add-inp" disabled="" placeholder="请选择" v-model="driver.years" />
			<!-- <image src="../../../static/select.png"></image> -->
			<picker @change="seleceYears" range-key="driver_age" :range="yearData" :value="index">
				<image src="../../../static/select.png"></image>
			</picker>
		</view>
		<button class="sure"  @click="$noMultipleClicks(fromsubmit)">确定</button>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				driver: {
					name: '', //姓名
					type: '', //驾照类型
					years: '', //司机驾龄
					address: '', //住址	
					age: '', //年龄
					addcar_type: '', //驾龄类型
					
				},
				//arrayType: ['A1', 'A2', 'A3', 'B1', 'B2', 'C1', 'C2', 'C3'], //驾照类型选择
				indexType: '',
				//arrayYears: ['一年以内', '一年到三年', '三年到五年', '五年到十年', '十年以上'], //驾龄选择
				indexYears: '',
				selectType: [], //驾照类型
				yearData: [], //驾龄
				index: '0',
				driverType_id:'',//驾照类型id
				driverAge_id:'',//驾龄id
				noClick:true
			}
		},
		onShow() {
			this.getType();
			this.getYears();
		},
		methods: {
			/*车辆类型选择 */
			seleceType: function(e) {
				let index = e.detail.value;
				let type = this.selectType[index];
				this.driver.addcar_type = type.type_name;
				//this.index = e.detail.value;
				this.driverType_id = type.type_id;
			},
			/* 驾龄选择 */
			seleceYears: function(e) {
				console.log(e)
				let idx = e.detail.value;
				let year = this.yearData[idx];
				this.driver.years = year.driver_age;
				this.driverAge_id = year.id;
				//this
				console.log(year);
			},
			getYears() {
				let self = this;
				uni.showLoading({
					title: '加载中'
				});
				self._get('car.index/get_driveage', {}, function(res) {
					console.log(res);
					self.yearData = res.data.list;
					uni.hideLoading();
				});
			},
			getType() {
				let self = this;
				uni.showLoading({
					title: '加载中'
				});
				self._get('home/getdrivertype', {}, function(res) {
					console.log(res.data.driver_type);
					self.selectType = res.data.driver_type;
					uni.hideLoading();
				});
			},
			// 页面跳转
			gotoPage(path) {
				let self = this;
				if (path.startsWith('/')) {
					uni.navigateTo({
						url: path
					});
				} else {
					self[path]();
				}
			},
			fromsubmit() {
				let self = this;
				//let user_id = JSON.parse(localStorage.getItem('user_id')).data
				//console.log(localStorage.getItem('token'))
				//let token = localStorage.getItem('token');
				let formData = {};

				let bb = /^([\u4e00-\u9fa5]{2,20}|([a-zA-Z]+\s?){2,20})$/;
				if (!bb.test(self.driver.name)) {
					uni.showToast({
						title: '请填写正确的姓名',
						duration: 1000,
						icon: 'none'
					});
					return false;
				}
				// if (self.driver.name == '') {
				// 	uni.showToast({
				// 		title: '请输入姓名',
				// 		duration: 1000,
				// 		icon: 'none'
				// 	});
				// 	return false;
				// }
				if (self.driver.address == '') {
					uni.showToast({
						title: '请输入住址',
						duration: 1000,
						icon: 'none'
					});
					return false;
				}
				if (self.driver.age == '') {
					uni.showToast({
						title: '请输入年龄',
						duration: 1000,
						icon: 'none'
					});
					return false;
				}
				if (self.driver.addcar_type == '') {
					uni.showToast({
						title: '请选择驾照类型',
						duration: 1000,
						icon: 'none'
					});
					return false;
				}
				if (self.driver.years == '') {
					uni.showToast({
						title: '请选择驾龄',
						duration: 1000,
						icon: 'none'
					});
					return false;
				}
				formData.realname = self.driver.name;
				formData.address = self.driver.address;
				formData.driver_age = self.driverAge_id;
				formData.age = self.driver.age;
				formData.driver_type = self.driverType_id;
				//formData.user_id = user_id;
				//formData.token = token;
				console.log(formData);
				//return;
				uni.showLoading({
					title: '正在提交',
				});
				self._post(
					'user.add/add_driver',
					formData,

					result => {
						console.log(result.code);
						if (result.code == 1) {
							uni.showToast({
								title: '添加成功！',
								duration: 2000,
								icon: 'none'
							});
							self.gotoPage('/pages/user/my-driver/my-driver');
						}
					},
					false,
					() => {
						uni.hideLoading();
					}
				)
			},
		}
	}
</script>

<style lang="scss">
	@import 'add-driver.scss';
</style>
